package com.gr.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gr.entity.*;
import com.gr.service.IAdminRoleService;
import com.gr.service.IRoleResourceService;
import com.gr.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author guoran
 * @since 2022-05-07
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IAdminRoleService adminRoleService;
    @Autowired
    private IRoleResourceService roleResourceService;
    @Autowired
    private IRoleService roleService;

    @PostMapping("/grantRidForUserId")
    public ResultEntity grantRidForUserId(@RequestBody AdminRoleDto dto){
        //删除老数据，添加新数据
        QueryWrapper<AdminRole> wrapper=new QueryWrapper<>();
        wrapper.eq("admin_id",dto.getUserId());
        adminRoleService.remove(wrapper);
        List<Integer> roleIds = dto.getRoleIds();
        //封装所有的AdminRole对象
        List<AdminRole> adminRoleList=new ArrayList<>();
        for (Integer rid:roleIds){
            AdminRole adminRole = new AdminRole();
            adminRole.setRoleId(rid);
            adminRole.setAdminId(dto.getUserId());
            adminRoleList.add(adminRole);
        }
        //批量保存所有的角色信息
        adminRoleService.saveBatch(adminRoleList);
        return ResultEntity.ok();
    }

    // 添加权限500报错  没加对象注解
    @PostMapping("/grantResource")
    public ResultEntity<RoleResource> grantResource(@RequestBody RoleResourceDto dto){
        //删除老数据添加新数据
        QueryWrapper<RoleResource> wrapper=new QueryWrapper<>();
        wrapper.eq("role_id",dto.getRoleId());
        roleResourceService.remove(wrapper);
        List<Integer> resourceIds = dto.getResourceIds();
        //封装所有的RoleResource对象
        List<RoleResource> roleResourceList=new ArrayList<>();
        for (Integer rid:resourceIds){
            RoleResource roleResource=new RoleResource();
            roleResource.setRoleId(rid);
            roleResource.setResourceId(dto.getRoleId());
            roleResourceList.add(roleResource);
        }
        //批量保存所有的权限信息
        roleResourceService.saveBatch(roleResourceList);
        return ResultEntity.ok();
    }

    @GetMapping("/selectAll")
    public ResultEntity<List<Role>> selectAll(){
        List<Role> roles = roleService.list();
        return ResultEntity.ok(roles);
    }

    // 添加报错500 没加对象注解
    @PostMapping("/add")
    public ResultEntity addRole(@RequestBody Role role){
        roleService.save(role);
        return ResultEntity.ok();
    }

    @DeleteMapping("/delete/{roleId}")
    public ResultEntity deleteRole(@PathVariable("roleId") Integer roleId){
        roleService.removeById(roleId);
        return ResultEntity.ok();
    }

    @GetMapping("/selectOne/{roleId}")
    public ResultEntity<Role> selectOne(@PathVariable("roleId")Integer roleId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("role_id",roleId);
        Role one = roleService.getOne(wrapper);
        return ResultEntity.ok(one);
    }

    @GetMapping("/selectResourcesByRoleId/{roleId}")
    public ResultEntity<List<Integer>> selectResourcesByRoleId(@PathVariable("roleId")Integer roleId){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("role_id",roleId);
        List<RoleResource> list = roleResourceService.list(wrapper);
        List<Integer> resList = list.stream().map((roleResource) -> {
            return roleResource.getResourceId();
        }).collect(Collectors.toList());
        return ResultEntity.ok(resList);
    }

    @PutMapping("/update")
    public ResultEntity update(@RequestBody Role role){
        roleService.updateById(role);
        return ResultEntity.ok();
    }

}

